﻿using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Caching;
using PmSoft.DBContext;
using PmSoft.Repositories;

namespace Modules.Person.Repositories;

[UseDI(ServiceLifetime.Scoped)]
public class PersonCertificateRepository : Repository<PersonCertificate, DefaultDbContext>
{
    public PersonCertificateRepository(DefaultDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
    {
    }

    public IEnumerable<PersonCertificate> GetPersonCertificatesByPersonId(int personId)
    {
        int areaVersion = EntityCacheService.GetAreaVersion("PersonId", personId);
        return GetTopEntities(1000, CachingExpirationType.ObjectCollection, () =>
        {
            return $"PersonCertificatesByPersonId:{personId}:{areaVersion}";
        }, () =>
        {
            Sql sql = Sql.Builder
            .Select("id")
            .From("person_certificate")
            .Where("person_id=@0", personId);
            return sql;
        });
    }
}